//
//  CPSCAllCategoryViewController.m
//  CollTest
//
//  Created by codew on 11/17/21.
//

#import "CPSCAllCategoryViewController.h"
#import "CPSCAllCategoryNavBarView.h"
#import "CPSCAll2CategoryLeftView.h"
#import "CPSCAll2CategoryRightView.h"
#import "CPSCAll2CategoryVC_Constants.h"
#import "CPSCAll2CategoryRightViewUIModel.h"
#import "CPSCAll2CategoryLeftCellUIModel.h"
#import "CPSCCategoryResultNavBarView.h"
@interface CPSCAllCategoryViewController ()


@property (nonatomic, strong) UIView * viewNavSuper;
@property (nonatomic, strong) UIView * viewLeftSuper;
@property (nonatomic, strong) UIView * viewRightSuper;

@property (nonatomic, strong) CPSCCategoryResultNavBarView * viewNavBar;
@property (nonatomic, strong) CPSCAll2CategoryLeftView * viewLeft;
@property (nonatomic, strong) CPSCAll2CategoryRightView * viewRight;

// ======Data
@property (nonatomic, strong) NSMutableArray * dataArrayLeft;

@end

@implementation CPSCAllCategoryViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUpMainUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)setUpMainUI
{
    self.view.backgroundColor = [UIColor colorWithHexString:@"#4974F5"];
    self.viewNavSuper.backgroundColor = [UIColor colorWithHexString:@"#4974F5"];
    self.viewLeftSuper.backgroundColor = [UIColor whiteColor];
    self.viewRightSuper.backgroundColor = [UIColor whiteColor];
    
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.navigationItem.leftBarButtonItem.customView.hidden = YES;
    self.navigationItem.titleView = [UIView new];
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    
    [self initSubviews];
    [self makeContraints];
}



#pragma mark - private initUI
//MARK:初始化
- (void)initSubviews
{
    [self.view addSubview:self.viewNavSuper];
    [self.view addSubview:self.viewLeftSuper];
    [self.view addSubview:self.viewRightSuper];
    
    [self.viewNavSuper addSubview:self.viewNavBar];
    [self.viewLeftSuper addSubview:self.viewLeft];
    [self.viewRightSuper addSubview:self.viewRight];
    [self deBugUI];
}

- (void)deBugUI
{
    self.viewNavSuper.backgroundColor = [UIColor colorWithHexString:@"#4974F5"];
    self.viewLeftSuper.backgroundColor = kCPSCRandomColor;
    self.viewRightSuper.backgroundColor = kCPSCRandomColor;
    
    CPSCAll2CategoryRightViewUIModel * model = [CPSCAll2CategoryRightViewUIModel dataDemo];
    [self.viewRight updateViewWithItem:model];
    
    NSArray <CPSCAll2CategoryLeftViewUIModelProtocol>* dataArray = CPSCAll2CategoryLeftCellUIModel.dataTest;
    [self.viewLeft updateViewDataSources:dataArray];
    
    self.viewNavBar.stringSearchKey = @"你瞅啥!";
}

- (void)makeContraints
{
    [self.viewNavSuper mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(kStatusBarHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(64);
    }];
    
    [self.viewLeftSuper mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewNavSuper.mas_bottom);
        make.bottom.left.mas_equalTo(0);
        make.width.mas_equalTo(KValueWidthCPSCAll2CategoryLeftView);
    }];
    
    [self.viewRightSuper mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewLeftSuper.mas_top);
        make.left.equalTo(self.viewLeftSuper.mas_right);
        make.bottom.right.mas_equalTo(0);
    }];
    

    //=======
    [self.viewNavBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    
    [self.viewLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];


    [self.viewRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
}



#pragma mark - Action

- (void)leftBlackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Getter Setter

- (UIView *)viewNavSuper
{
    if (!_viewNavSuper) {
        
        UIView * view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
                
        _viewNavSuper = view;
    }
    return _viewNavSuper;
}

- (UIView *)viewLeftSuper
{
    
    if (!_viewLeftSuper) {
        
        UIView * view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
                
        _viewLeftSuper = view;
    }
    return _viewLeftSuper;
}

- (UIView *)viewRightSuper
{
    
    if (!_viewRightSuper) {
        
        UIView * view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        view.clipsToBounds = YES;
        
        _viewRightSuper = view;
    }
    return _viewRightSuper;
}

- (CPSCCategoryResultNavBarView *)viewNavBar
{
    DggWEAKSELF(weakSelf);
    if (!_viewNavBar) {
        
        CPSCCategoryResultNavBarView * view = [[CPSCCategoryResultNavBarView alloc] init];
        view.backgroundColor = [UIColor colorWithHexString:@"#4974F5"];;
        view.clipsToBounds = YES;
                
        view.blockLeftBlock = ^{
          
            [weakSelf leftBlackAction];
        };
        
        _viewNavBar = view;
    }
    return _viewNavBar;
}

- (CPSCAll2CategoryLeftView *)viewLeft
{
    DggWEAKSELF(weakSelf);
    if (!_viewLeft) {
        
        CPSCAll2CategoryLeftView * view = [CPSCAll2CategoryLeftView viewCategoryLeft];
        view.backgroundColor = [UIColor whiteColor];
                
        
        view.blockLeftClick = ^(id<CPSCAll2CategoryLeftViewUIModelProtocol>  _Nonnull item) {
          
            NSLog(@"stringTitleTopCategory: %@", item.stringTitleTopCategory);
            /**
             1. 先隐藏Right
             */
            [weakSelf.viewRight resetView];
            // TODO: 2021年11月17日16:24:58 到时候真实数据
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                CPSCAll2CategoryRightViewUIModel * model = [CPSCAll2CategoryRightViewUIModel dataDemo];
                [weakSelf.viewRight updateViewWithItem:model];
            });
            
        };
        _viewLeft = view;
    }
    return _viewLeft;
}


- (CPSCAll2CategoryRightView *)viewRight
{
    
    DggWEAKSELF(weakSelf);
    if (!_viewRight) {
        
        CPSCAll2CategoryRightView * view = [CPSCAll2CategoryRightView viewRight];
        view.backgroundColor = [UIColor whiteColor];
                
        view.blockSelectRecommend = ^(id<CPSCAll2CategoryRightViewUIModelProtocol>  _Nonnull dataItem) {
            
            NSLog(@"CPSCAll2CategoryViewController blockSelectRecommend: %@", dataItem.stringTitleRecommend);
        };
        
        
        view.blockCategory = ^(id<CPSCAll2CategoryRightViewUIModelProtocol>  _Nonnull dataItem) {
            
            NSLog(@"CPSCAll2CategoryViewController blockCategory: %@", dataItem.stringTitleSecond);
        };
        
        view.blockSwitch = ^(CPSCAll2CategoryViewSwitchType type) {
          
            if (CPSCAll2CategoryViewSwitchType_Next == type) {
                
                NSLog(@"blockSwitch: 下一个........");
            }else{
                
                NSLog(@"blockSwitch: 换回上一个");
            }
            
            [weakSelf.viewLeft upateViewWtihSwitchType:type];
        };
        
        _viewRight = view;
    }
    return _viewRight;
}


@end
